package com.mrx.xcitools.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import static com.mrx.xcitools.constant.FileConstant.JARS_DIR;

/**
 * @author Mr.X
 * @since 2024-03-11 14:34
 **/
@Service
public class FileUploadService {

    @Resource
    private JarFileManageService jarFileManageService;

    private static final Logger logger = LoggerFactory.getLogger(FileUploadService.class);

    public void uploadJar(MultipartFile file, String fileName) throws IOException {
        logger.info("开始处理上传的 jar: {}", fileName);
        File uploadedFile = new File(JARS_DIR, fileName);
        FileCopyUtils.copy(file.getInputStream(), Files.newOutputStream(uploadedFile.toPath()));
        logger.info("{} 上传完毕", fileName);
        jarFileManageService.addJarFile(uploadedFile);
    }

}
